﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SpeakupBackend.Themes;

namespace SpeakupTests.SpeakupBackend.Themes.Workshop
{
    [TestClass]
    public class WorkshopThemeBuilderTests
    {
        [TestMethod]
        public void CreateWorkshopTheme()
        {
            WorkshopClassType classType = WorkshopClassType.GetWorkshopClassTypes().First();
            LevelGroup levelGroup = LevelGroup.DefaultFactory.GetLevelGroups().First();
            Week week = Week.GetWeeks().First();
            String name = "name1";

            foreach (WorkshopClassType ct in WorkshopClassType.GetWorkshopClassTypes())
            {
                WorkshopThemeBuilder builder = new WorkshopThemeBuilder();
                builder.ClassType = ct;
                builder.LevelGroup = levelGroup;
                builder.Week = week;
                builder.Name = name;

                WorkshopTheme theme = builder.Create();                   
                Assert.AreEqual(ct, theme.ClassType);
            }

            foreach (LevelGroup lg in LevelGroup.DefaultFactory.GetLevelGroups())
            {
                WorkshopThemeBuilder builder = new WorkshopThemeBuilder();
                builder.ClassType = classType;
                builder.LevelGroup = lg;
                builder.Week = week;
                builder.Name = name;

                WorkshopTheme theme = builder.Create();
                Assert.AreEqual(lg, theme.LevelGroup);
            }

            foreach (Week w in Week.GetWeeks())
            {

                WorkshopThemeBuilder builder = new WorkshopThemeBuilder();
                builder.ClassType = classType;
                builder.LevelGroup = levelGroup;
                builder.Week = w;
                builder.Name = name;

                WorkshopTheme theme = builder.Create();
                Assert.AreEqual(w, theme.Week);

                WorkshopTheme wsth = new WorkshopTheme(classType, levelGroup, w, "some thName");
                Assert.AreEqual(w, wsth.Week);
            }

            Random rnd = new Random();
            for (int i = 0; i < 10; i++)
            {
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < 10; j++)
                {
                    sb.Append(rnd.Next('a', 'z'));
                }


                WorkshopThemeBuilder builder = new WorkshopThemeBuilder();
                builder.ClassType = classType;
                builder.LevelGroup = levelGroup;
                builder.Week = week;
                builder.Name = sb.ToString();

                WorkshopTheme theme = builder.Create();
                Assert.AreEqual(sb.ToString(), theme.Name);
            }
        }

        [TestMethod]
        public void CreateInvalidWorkshopTheme()
        {
            WorkshopClassType classType = WorkshopClassType.GetWorkshopClassTypes().First();
            LevelGroup levelGroup = LevelGroup.DefaultFactory.GetLevelGroups().First();
            Week week = Week.GetWeeks().First();
            String name = "thName";

            //null class type
            {
                WorkshopThemeBuilder builder = new WorkshopThemeBuilder();
                builder.ClassType = null;
                builder.LevelGroup = levelGroup;
                builder.Week = week;
                builder.Name = name;

                bool thrown = false;
                try
                {
                    builder.Create();
                }
                catch (ArgumentException)
                {
                    thrown = true;
                }

                Assert.AreEqual(true, thrown);
            }

            //null level group
            {
                WorkshopThemeBuilder builder = new WorkshopThemeBuilder();
                builder.ClassType = classType;
                builder.LevelGroup = null;
                builder.Week = week;
                builder.Name = name;

                bool thrown = false;
                try
                {
                    builder.Create();
                }
                catch (ArgumentException)
                {
                    thrown = true;
                }

                Assert.AreEqual(true, thrown);
            }

            //null week
            {
                WorkshopThemeBuilder builder = new WorkshopThemeBuilder();
                builder.ClassType = classType;
                builder.LevelGroup = levelGroup;
                builder.Week = null;
                builder.Name = name;

                bool thrown = false;
                try
                {
                    builder.Create();
                }
                catch (ArgumentException)
                {
                    thrown = true;
                }

                Assert.AreEqual(true, thrown);
            }

            //null week
            {
                WorkshopThemeBuilder builder = new WorkshopThemeBuilder();
                builder.ClassType = classType;
                builder.LevelGroup = levelGroup;
                builder.Week = week;
                builder.Name = null;

                bool thrown = false;
                try
                {
                    builder.Create();
                }
                catch (ArgumentException)
                {
                    thrown = true;
                }

                Assert.AreEqual(true, thrown);
            }
        }
    }
}
